package com.inews.business.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inews.base.mapper.JsonMapper;
import com.inews.base.security.service.ResourceCacheService;
import com.inews.base.security.vo.SecurityUserDetail;
import com.inews.base.security.vo.TreeNode;
import com.inews.base.utils.InewsUtils;

@Controller
public class LoginController {
	
	private SecurityUserDetail userDetail;
	
	@Autowired
	private ResourceCacheService resourceCacheService;
	
	/**
	 * 用户登录成功.
	 *
	 * @return the string
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/login")
	public String login(Model model) throws Exception {
		return "index";
	}
	
	
	/**
	 * 用户登录失败.
	 *
	 * @return the string
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/loginFailure")
	public String loginFailure(Model model) throws Exception {
		return "login";
	}
	
	
	/**
	 * 欢迎页面.
	 *
	 * @return the string
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/welcome")
	public String welcome(Model model) throws Exception {
		return "welcome";
	}
	
	
	/**
	 * 获取菜单树.
	 *
	 * @return the string
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/getMenuTree")
	@ResponseBody
	public Object getMenuTree() throws Exception {
		//得到用户信息和用户的权限列表
		userDetail = (SecurityUserDetail)InewsUtils.getUserInfo();
		List<TreeNode> treeNodes = resourceCacheService.buileMenuTree(userDetail.getAuthorities());
		String treeNodeJson = JsonMapper.nonEmptyMapper().toJson(treeNodes);
		return treeNodeJson;
	}
	
	
	
	/**
	 * session 超时页面.
	 *
	 * @return the string
	 */
	@RequestMapping("/sessionExpired")
	public String sessionExpired(){
		return "sessionExpired";
	}

}
